package osb.map.main;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import com.ljp.ani.R;
import osb.map.main.Comment_ListView.OnRefreshListener;
import osb.map.net.ClientSocket;
import osb.map.net.DataComment;
import osb.map.net.DataPackage;
import osb.map.net.msgType;
import osb.map.publicdata.Public_Operate_And_Data;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 评论activity
 * @author Administrator
 *
 */

public class Comment extends Activity
{
	public static Comment public_Static_cCommentActivity;
	 private LinkedList<DataPackage> data_content; // 内容

	private BaseAdapter adapter;
	private ImageView iamgeview;        // 列表开头现实的图片
	private Button mycommentButton;     // 发表按钮
	private Button BackButton;          // 返回的按钮
	LinearLayout LinearLayout_menu;     // 弹出的菜单
	boolean IsShowMenu = false;         // 是否弹出菜单

	static DataPackage DataPackage_test;

	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comment);
		public_Static_cCommentActivity = this;

		data_content = new LinkedList<DataPackage>();
		iamgeview = (ImageView) findViewById(R.id.imageView_item); // 获得列表开头现实的图片 
		mycommentButton = (Button) findViewById(R.id.mycomment); // 发表评论的按钮
		LinearLayout_menu = (LinearLayout) findViewById(R.id.menu);
		BackButton = (Button) findViewById(R.id.comment_back);
		
		// 返回按钮
		BackButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v) 
			{
				Comment.this.finish();
			}
		});
		// 发表评论
		mycommentButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (!Public_Operate_And_Data.Islogin)
				{
					new AlertDialog.Builder(Comment.this).setMessage("您需要登录才能执行此操作").setPositiveButton("确定", null).show();
					return;
				}
				
				
				final Dialog dialog = new Dialog(v.getContext(), R.style.dialog);
			      dialog.setContentView(R.layout.dialog_exit);
			      dialog.setCancelable(true);

			      final Window dialogWindow = dialog.getWindow();
			      dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP);

			      dialog.show();

			      WindowManager m = getWindowManager();
			      Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
			      WindowManager.LayoutParams p = getWindow().getAttributes(); 
                  p.y = -300;
			      p.height = (int) (d.getHeight() * 0.5);
			      p.width = (int) (d.getWidth() * 1); // 宽度设置为屏幕的0.95
			      dialogWindow.setAttributes(p);

			 
			      
			      Button ok = (Button) dialogWindow.findViewById(R.id.btn_ok);
			      ok.setOnClickListener(new View.OnClickListener() {
			        public void onClick(View v) {
			        	userId = Public_Operate_And_Data.User_id;
						String Name = Public_Operate_And_Data.User_name;
						
						EditText textView = (EditText) dialogWindow.findViewById(R.id.et_content);
						
						comcontent = textView.getText().toString();

						if (comcontent.equals("")) 
						{
							new AlertDialog.Builder(
									Comment.this)
									.setMessage("内容为空")
									.setPositiveButton("确定",
											null).show();
							return;
						}
						
						Date now = new Date(System.currentTimeMillis());
				    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				    	String msgTime = format.format(now);
						
						DataPackage data = new DataPackage(msgType.Comment_User_Post);
						data.dataComment = new DataComment(userId, Name, comcontent, msgTime);
						ClientSocket.Public_Static_ClientSocket.sendMsg(data);
						
                        dialog.cancel();
			        }
			      });
			      Button cancel = (Button) dialogWindow.findViewById(R.id.btn_cancel);
			      cancel.setOnClickListener(new View.OnClickListener() {
			        public void onClick(View v) {
			          
			          dialog.cancel();
			        }

			      });
			      Button btn_exitback = (Button) dialogWindow.findViewById(R.id.btn_exitback);
			      btn_exitback.setOnClickListener(new View.OnClickListener() {
			        public void onClick(View v) {
			          
			          dialog.cancel();
			        }

			      });
			      Button mycomment = (Button) dialogWindow.findViewById(R.id.mycomment);
			      mycomment.setOnClickListener(new View.OnClickListener() {
				        public void onClick(View v) {
				          
				        	userId = Public_Operate_And_Data.User_id;
							String Name = Public_Operate_And_Data.User_name;
							
							EditText textView = (EditText) dialogWindow.findViewById(R.id.et_content);
							
							comcontent = textView.getText().toString();

							if (comcontent.equals("")) 
							{
								new AlertDialog.Builder(
										Comment.this)
										.setMessage("内容为空")
										.setPositiveButton("确定",
												null).show();
								return;
							}
							
							Date now = new Date(System.currentTimeMillis());
					    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					    	String msgTime = format.format(now);
							
							DataPackage data = new DataPackage(msgType.Comment_User_Post);
							data.dataComment = new DataComment(userId, Name, comcontent, msgTime);
							ClientSocket.Public_Static_ClientSocket.sendMsg(data);
							
	                        dialog.cancel();
				        }

				      });
			}
		});
		// 初始化comment数据
		for (int i = 0; i < Public_Operate_And_Data.Vector_comment.size(); i++)
		{
			DataPackage dataPackage = Public_Operate_And_Data.Vector_comment.get(i);
			data_content.add(dataPackage);
		}

		final Comment_ListView listView = (Comment_ListView) findViewById(R.id.listView);
		adapter = new BaseAdapter()
		{
			public View getView(int position, View convertView, ViewGroup parent)
			{
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.comment_item, null);

				// 设置显示名字
				TextView textView_content = (TextView) convertView.findViewById(R.id.textView_name);
				textView_content.setText(data_content.get(position).dataComment.getName());

				// 设置显示内容 中
				TextView textView_time = (TextView) convertView.findViewById(R.id.textView_item);
				textView_time.setText(data_content.get(position).dataComment.getComcontent());

				// 设置显示时间 上
				TextView textView_name = (TextView) convertView.findViewById(R.id.textView_time);
				textView_name.setText(data_content.get(position).dataComment.getSendtime());

				// 设置头像图片
				// ImageView ImageView_Item = (ImageView) convertView
				// .findViewById(R.id.imageView_item);
				// ImageView_Item.setImageResource(R.drawable.ccc);

				return convertView;
			}

			public long getItemId(int position)
			{
				return position;
			}

			public Object getItem(int position)
			{
				return data_content.get(position);
			}

			public int getCount()
			{
				return data_content.size();
			}
		};
		listView.setAdapter(adapter);

		listView.setonRefreshListener(new OnRefreshListener()
		{
			public void onRefresh()
			{
				new AsyncTask<Void, Void, Void>()
				{
					protected Void doInBackground(Void... params)
					{
						try
						{
							Thread.sleep(1000);
						} catch (Exception e)
						{
							e.printStackTrace();
						}
						
						return null;
					}

					@Override
					protected void onPostExecute(Void result)
					{
						adapter.notifyDataSetChanged();
						listView.onRefreshComplete();
					}

				}.execute(null);
			}
		});
	}

	/**
	 * 刷新comment内容
	 * @param dataPackage
	 */
	public void Refresh(DataPackage dataPackage)
	{
		Public_Operate_And_Data.Vector_comment.addFirst(dataPackage);
		data_content.addFirst(dataPackage);
	}

	private String userId;
	private String comcontent;

	/**
	 * 发表失败
	 */
	public void PostcomWrong()
	{
		new AlertDialog.Builder(Comment.this).setTitle("错误").setMessage("发表失败")
				.setPositiveButton("确定", null).show();
	}

	/**
	 * 刷新失败
	 */
	public void RefreshWrong()
	{
		new AlertDialog.Builder(Comment.this).setTitle("错误").setMessage("刷新失败")
				.setPositiveButton("确定", null).show();
	}

//	// 点击MENU 事件 执行全屏视图
//	@Override
//	public boolean onPrepareOptionsMenu(Menu menu)
//	{
//		if (IsShowMenu)
//		{
//			LinearLayout_menu.setVisibility(View.INVISIBLE);
//			IsShowMenu = false;
//		} else
//		{
//			LinearLayout_menu.setVisibility(View.VISIBLE);
//			IsShowMenu = true;
//		}
//		return true;
//	}
	
    /**
	 * menu
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE, Menu.FIRST + 1, 3, "分享下载").setIcon(android.R.drawable.ic_menu_send);
		return true;

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case Menu.FIRST + 1:
			Intent intent = new Intent();
			intent.setClass(Comment.this, ShareActivity.class);
			Comment.this.startActivity(intent);

			break;
		}
		return false;
	}
}